class Employee {
   Integer employeeId//
    String firstName//
    String lastName//
    Date dob//
    String title//
    Double salary//
    String email//
    String street//
    String city
    Integer zipCode
    String userName//
    String password//
    String passwordVerify//
    Boolean admin//
    Float percentageWork = 100.0//

    static belongsTo = Project
    static hasMany = [projects : Project]

    static Integer workingHoursBellow = 1732
    static Integer workingHoursOver = 1700

    static Integer fortyYears = 40*365+10

    Integer getWorkingHoursPerYear() {
        boolean isOverForty = false
        Date today = new Date()
        if (dob != null && today.minus(dob) >= fortyYears) {
            isOverForty = true
        }
        if (isOverForty) {
            return percentageWork * workingHoursOver / 100
        } else {
            return percentageWork * workingHoursBellow / 100
        }
    }

    static transients = ['passwordVerify', 'workingHoursPerYear']

    static constraints = {
        //personal informations
        firstName(size:2..20,matches:"[a-zA-Z ^¨éèàöäÖÄÅåêëñïîíÑßØÆ-]+",blank:false)
        lastName(size:2..20,matches:"[a-zA-Z ^¨éèàöäÖÄÅåêëñïîíÑßØÆ-]+",blank:false)
        street(size:2..20,matches:"[a-zA-Z0-9 ^¨éèàöäÖÄÅåêëñïîíÑßØÆ-]+",blank:false)
        city(size:2..20,matches:"[a-zA-Z ^¨éèàöäÖÄÅåêëñïîíÑßØÆ-]+",blank:false)
        zipCode(min:10000,max:99999,blank:false)
        dob(max:new Date())
        email(size:5..50,email:true,blank:false)

        //login informations
        userName(size:1..10,unique:true,blank:false)
        password(size:1..50,blank:false)
        passwordVerify(size:1..50,blank:false, validator:{val, obj ->
            if(!val?.equals(obj.password)) {
                return "employee.passwordVerify.mismatch"
            }
        })

        //professional informations
        admin()
        title(size:1..20,blank:false)
        salary(matches:"[0-9., ]+",min:0d,max:999999d,blank:false)
        percentageWork(min:0f,max:100f)
        employeeId(blank:false)
    }

    String toString(){"${this.firstName} ${this.lastName}, (${this.userName})"}
}
